package net.hvacapp.cmvrfforandroid.ui.fragment;


import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.ui.project.model.ProjectInfoData;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * A simple {@link Fragment} subclass.
 */
@EFragment(R.layout.fragment_project)
public class ProjectFragment extends BaseFragment implements TabLayout.OnTabSelectedListener{

    @ViewById(R.id.project_tablayout)
    TabLayout mProjectTabL;

    ProjectListFragment mFragmentAll;
    ProjectListFragment mFragmentInProgress;
    ProjectListFragment mFragmentDeal;
    ProjectListFragment mFragmentNotDeal;
    ProjectListFragment mFragmentSub; //子用户项目

    public ProjectFragment() {

    }

    @AfterViews
    public void init(){

        mProjectTabL.addTab(mProjectTabL.newTab().setText("全部"), true);
        mProjectTabL.addTab(mProjectTabL.newTab().setText("进行中"), false);
        mProjectTabL.addTab(mProjectTabL.newTab().setText("未成交"), false);
        mProjectTabL.addTab(mProjectTabL.newTab().setText("已成交"), false);
        mProjectTabL.addTab(mProjectTabL.newTab().setText("下级用户"), false);


        mProjectTabL.setOnTabSelectedListener(this);


        setTabSelection(0);
        
    }


    /**
     * 设置tab页
     * @param position 0:概况  1:关键词  2:排名
     */
    private void setTabSelection(int position) {
        // 开启一个Fragment事务
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);

        switch (position) {
            case 0:
                if (mFragmentAll == null) {
                    mFragmentAll = ProjectListFragment_.builder().arg("typeValue", ProjectInfoData.PROJECT_PHASE_ALL).build();
                    transaction.add(R.id.project_content_fl, mFragmentAll);
                } else {
                    transaction.show(mFragmentAll);
                }
                break;
            case 1:
                if (mFragmentInProgress == null) {
                    mFragmentInProgress = ProjectListFragment_.builder().arg("typeValue", ProjectInfoData.PROJECT_PHASE_INPROGRESS).build();
                    transaction.add(R.id.project_content_fl, mFragmentInProgress);
                } else {
                    transaction.show(mFragmentInProgress);
                }
                break;
            case 2:
                if (mFragmentNotDeal == null) {
                    mFragmentNotDeal = ProjectListFragment_.builder().arg("typeValue", ProjectInfoData.PROJECT_PHASE_NOTDEAL).build();
                    transaction.add(R.id.project_content_fl, mFragmentNotDeal);
                } else {
                    transaction.show(mFragmentNotDeal);
                }
                break;
            case 3:
                if (mFragmentDeal == null) {
                    mFragmentDeal = ProjectListFragment_.builder().arg("typeValue", ProjectInfoData.PROJECT_PHASE_DEAL + ProjectInfoData.PROJECT_PHASE_REVIEW).build();
                    transaction.add(R.id.project_content_fl, mFragmentDeal);
                } else {
                    transaction.show(mFragmentDeal);
                }
                break;
            case 4:
                if (mFragmentSub == null) {
                    mFragmentSub = ProjectListFragment_.builder().arg("typeValue", ProjectInfoData.PROJECT_SUB).build();
                    transaction.add(R.id.project_content_fl, mFragmentSub);
                } else {
                    transaction.show(mFragmentSub);
                }
                break;
        }

        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (mFragmentAll != null) {
            transaction.hide(mFragmentAll);
        }
        if (mFragmentInProgress != null) {
            transaction.hide(mFragmentInProgress);
        }
        if (mFragmentNotDeal != null) {
            transaction.hide(mFragmentNotDeal);
        }
        if (mFragmentDeal != null) {
            transaction.hide(mFragmentDeal);
        }
        if (mFragmentSub != null) {
            transaction.hide(mFragmentSub);
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                setTabSelection(0);
                break;
            case 1:
                setTabSelection(1);
                break;
            case 2:
                setTabSelection(2);
                break;
            case 3:
                setTabSelection(3);
                break;
            case 4:
                setTabSelection(4);
                break;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

}
